<?php
namespace Common\Model;

/**
 * 客栈个性推荐标签信息表
 * @modify trainyao 2016-04-28  创建
 * @index hotelspecialtag(`isdelete`, `hotelentity_id`);
 */

Class HotelSpecialTagModel extends BaseModel
{
    protected $trueTableName = 'bk_hotel_special_tag';

    /**
     * 根据客栈获取信息
     * @modify trainyao 2016-04-28 创建
     * @param  integer   $hotelId   客栈id
     * @return array              信息数组
     */
    public function getInfoByHotel($hotelId) {
        $option = array(
            'isdelete' => NOT_DELETE,
            'hotelentity_id' => $hotelId,
        );
        $data = $this->where($option)->select();

        return empty($data) ? array() : $data;
    }

    /**
     * 删除客栈标签id
     * @modify trainyao 2015-12-31  创建
     * @param  mixin   $id   一个或多个id
     * @return boolean 执行结果
     */
    public function deleteInfo($id) {
        $option = array();
        if (is_array($id)) {
            $id = implode(',', $id);
        }
        if (strpos($id, ',') === false) {
            $option['id'] = $id;
        } else {
            $option['id'] = array('in', $id);
        }
        $option['isdelete'] = NOT_DELETE;

        $data = array(
            'isdelete' => IS_DELETE,
        );
        
        return $this->where($option)->save($data) === false ? false : true;
    }

    /**
     * 增加客栈个性标签(一个或多个)
     * @modify trainyao 2015-12-31  创建
     * @param  integer   $hotelId    客栈id
     * @param  mixin   $tag      标签名
     * @return boolean 执行结果
     */
    public function addInfo($hotelId, $tag) {
        $data = array();
        $result = null;
        if (!is_array($tag) && strpos($tag, ',') !== false) {
            $tag = explode(',', $tag);
        }
        
        if (is_array($tag)) {
            $creator = getUserId();
            foreach ($tag as $tagItem) {
                array_push($data, array(
                    'hotelentity_id' => $hotelId,
                    'name' => $tagItem,
                    'creator' => $creator,
                    'operator' => $creator,
                ));
            }
            $result = $this->addAll($data);
        } else {
            $data['hotelentity_id'] = $hotelId;
            $data['name'] = $tag;
            $result = $this->add($data);
        }
        
        return $result == false ? false : true;
    }
}
